"""
@Author：十
@Time：2025/9/9 9:45
@FileName：filter.py
@Description：主界面的筛选边栏
"""
import os
import sys

from PyQt5.QtCore import QDateTime, QTime
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QWidget, QApplication

from UI.fliter import Ui_Sidebar
import resource.WTC_rc


class FilterWindow(QWidget, Ui_Sidebar):

    def __init__(self, parent=None):
        super(FilterWindow, self).__init__(parent)

        self.parent = parent

        self.setupUi(self)

        self.pushButton_Clear.clicked.connect(self.init_interface)
        self.pushButton_Confirm.clicked.connect(self.confirm_filter)

        palette = self.palette()
        palette.setColor(QPalette.Window, QColor("#2c3e50"))
        self.setPalette(palette)
        self.setAutoFillBackground(True)

        self.init_interface()

        self.confirm_filter()

    def resource_path(self, relative_path):
        """获取资源文件的绝对路径"""
        try:
            # PyInstaller创建临时文件夹，将路径存储在_MEIPASS中
            base_path = sys._MEIPASS
        except Exception:
            base_path = os.path.abspath(".")

        return os.path.join(base_path, relative_path)

    def init_interface(self):
        # 在setupUi之后添加
        # 只针对窗口背景设置样式，不影响子控件
        # self.setStyleSheet('''
        # QWidget#Form { background-color: #34495e; }
        # QLabel { color: white; }
        # QComboBox {
        # border: 2px solid #2c3a4a;
        # border-radius: 5px;
        # padding: 5px 10px;
        # background-color: #101c2d;
        # min-height: 25px;
        # color: rgb(89, 118, 150);
        # }
        # QComboBox::down-arrow {
        #     image: url(:/icon/down_arrow_2.png);
        #     width: 15px;
        #     height: 15px;
        # }
        # QComboBox::drop-down {
        #     subcontrol-origin: padding;
        #     subcontrol-position: right center;
        #     width: 25px;
        #     border: none;
        # }
        # QComboBox QAbstractItemView {
        #     border: 1px solid #101c2d;
        #     selection-background-color: #C5CAE9;
        #     padding-top: 2px;
        #     padding-bottom: 2px;
        #     background-color: #101c2d;
        #     color: white;
        # }
        # QComboBox:hover {
        #     border-color: #4D85D1;
        # }
        #
        # QComboBox:disabled {
        #     background-color: #F0F0F0;
        #     color: #999;
        # }
        # ''')

        # self.setStyleSheet("background-color: #34495e;")

        self.setAutoFillBackground(True)

        # 时间选择器
        current_datetime = QDateTime.currentDateTime()
        current_date = current_datetime.date()
        end_of_day_time = QTime(23, 59)
        end_of_day_datetime = QDateTime(
            current_date,
            end_of_day_time
        )
        self.dateTimeEdit_End.setDateTime(end_of_day_datetime)
        self.dateTimeEdit_End.setMinimumDateTime(self.dateTimeEdit_Start.dateTime())
        self.dateTimeEdit_Start.dateTimeChanged.connect(lambda: self.dateTimeEdit_End.setMinimumDateTime(self.dateTimeEdit_Start.dateTime()))

        self.doubleSpinBox_Inner_End.setMaximum(9999.99)  # 设置最大值
        self.doubleSpinBox_Inner_Start.setMaximum(9999.99)
        self.doubleSpinBox_Inner_End.setValue(120.00)

        self.doubleSpinBox_External_End.setMaximum(9999.99)
        self.doubleSpinBox_External_Start.setMaximum(9999.99)
        self.doubleSpinBox_External_End.setValue(120.00)

        self.doubleSpinBox_Ambient_End.setMaximum(9999.99)
        self.doubleSpinBox_Ambient_Start.setMaximum(9999.99)
        self.doubleSpinBox_Ambient_End.setValue(120.00)

        self.widget_10.setVisible(False)
        self.widget_12.setVisible(False)
        self.line_5.setVisible(False)

    def confirm_filter(self):
        try:
            operating = self.comboBox_Operating.currentText()
            if operating == '全部':
                state = ['正常', '异常', '离线']
            else:
                state = [operating]

            # filter_parameters = {
            #     'start_time': self.dateTimeEdit_Start.dateTime().toString('yyyy-MM-dd hh:mm:ss'),
            #     'end_time': self.dateTimeEdit_End.dateTime().toString('yyyy-MM-dd hh:mm:ss'),
            #     'inner_start': self.doubleSpinBox_Inner_Start.value(),
            #     'inner_end': self.doubleSpinBox_Inner_End.value(),
            #     'external_start': self.doubleSpinBox_External_Start.value(),
            #     'external_end': self.doubleSpinBox_External_End.value(),
            #     'ambient_start': self.doubleSpinBox_Ambient_Start.value(),
            #     'ambient_end': self.doubleSpinBox_Ambient_End.value(),
            #     'operating': state
            # }

            filter_parameters = [
                self.dateTimeEdit_Start.dateTime().toString('yyyy-MM-dd hh:mm:ss'),
                self.dateTimeEdit_End.dateTime().toString('yyyy-MM-dd hh:mm:ss'),
                self.doubleSpinBox_Inner_Start.value(),
                self.doubleSpinBox_Inner_End.value(),
                self.doubleSpinBox_External_Start.value(),
                self.doubleSpinBox_External_End.value(),
                self.doubleSpinBox_Ambient_Start.value(),
                self.doubleSpinBox_Ambient_End.value()
            ]

            filter_parameters = filter_parameters + state

            # print(filter_parameters)

            self.parent.Signal_Get_filter_parameters.emit(filter_parameters)
        except Exception as e:
            print('传递参数错误：', e)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = FilterWindow()
    mainWindow.show()
    sys.exit(app.exec_())
